﻿
package extremefx.i18n {
	/**
	 * @author Marcelo Volmaro
	 */
	internal class CCHijri {
		private static const EPOCH:int = 227014;
		
		public static function _isLeapYear(pYear:int):Boolean {
			return CCMath.mod(14+11*pYear, 30) < 11;
		}
		
		public static function _fixedFromDMY(pDay:int, pMonth:int, pYear:int):int {
			var k:int = EPOCH - 1;
			k += 354 * (pYear-1);
			k += CCMath.div(3+11*pYear, 30);
			k += int(Math.ceil(29.5 * Number(pMonth-1)));
			return k + pMonth;
		}
		
		public static function _yearFromFixed(pDate:int):int{
			return CCMath.div(30*(pDate - EPOCH) + 10646, 10631);
		}
		
		private static function _myFromFixed(pDate:int):Array {
			var year:int = _yearFromFixed(pDate);
			var m:int = 1 + int (Math.ceil((Number(pDate - 29 - _fixedFromDMY(1,1,year)))/29.5));
			
			return [year, m < 12 ? m : 12]; 
		}
		
		public static function _dmyFromFixed(pDate:int):Array {
			var out:Array = _myFromFixed(pDate);
			out.push(pDate - _fixedFromDMY(1, int(out[1]), int(out[0])) + 1);
			return out;
		}
		
		public static function _monthFromFixed(pDate:int):int {
			var out:Array = _myFromFixed(pDate);
			return int(out[1]);
		}
		
		public static function _dayFromFixed(pDate:int):int {
			var out:Array = _dmyFromFixed(pDate);
			return int(out[2]);
		}
	}
}
